package org.paradise.dms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.paradise.dms.pojo.MessageOut;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.BaseHibernateDao;
import com.dheaven.framework.dao.DaoException;
/**
 * 
 * Description: 发件箱
 * 
 * @version1.1 李韦 
 * @return
 */
@Service
public class MessageOutDaoImpl extends BaseHibernateDao<MessageOut>{
	private static Logger log = Logger.getLogger(MessageOutDaoImpl.class);
	
	/**
	 * 
	 * Description: 增加一条发件箱信息
	 * 
	 * @Version1.0李韦创建
	 * @throws DaoException
	 */
	public void insertMessage(MessageOut message) throws DaoException {
		this.create(message);
		log.info("成功插入发件箱,收件人为：：：：：：："+message.getReceivertelephone());
	}
	
	/**
	 * 
	 * Description: 删除一条发件箱信息
	 * 
	 * @Version1.0 李韦创建
	 * @throws DaoException
	 */
	public String deleteMessage(final long id) {
		return (String) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(org.hibernate.Session s)
					throws HibernateException, SQLException {
				String statusMsg = "";
				int sm = 0;
				SQLQuery query = (SQLQuery) s
						.createSQLQuery("delete respstation.*,    messageout.*,  deliverstation.*   from respstation left join  messageout on " +
							"messageout.messagesend = respstation.sendId " +
							"left join deliverstation on " +
							"respstation.submitReserve = deliverstation.msgContent " +
							"where messageout.id=?");
					query.setParameter(0, id);
					sm += query.executeUpdate();
					System.out.println(sm);
					if(sm >0 ){
						return "成功";
					}		
				return null;
			}
		});
	}
	
	


	/**
	 * 
	 * Description: 列出发件箱的所有信息
	 * 
	 * @version1.1 李韦 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MessageOut> listAllMessageOut() {
		List<MessageOut> list = null;
		list = this.find("from MessageOut");
		return list;
	}


/**
 * 
 * Description: 返回发件箱总条数
 * 
 * @Version1.0李韦创建
 * @return
 */
public int getMessageOutRows() {
	return this.find("from MessageOut").size();
}

/**
 * 
 * Description: 返回发件箱成功的总条数
 * 
 * @Version1.0 李韦创建
 * @return
 */
public int getMessageOutSuccessRows() {
	return this.find("from MessageOut ").size();
}

/**
 * 
 * Description: 发件箱列表
 * 
 * 李韦创建
 * @return
 */
@SuppressWarnings("unchecked")
public List getMessageList(int pageSize, int startRow) {
	final int pageSize1 = pageSize;
	final int startRow1 = startRow;
	return getHibernateTemplate().executeFind(new HibernateCallback() {
		public Object doInHibernate(org.hibernate.Session s)
				throws HibernateException, SQLException {
			String sql = "select MessageOut.id,MessageOut.receivertelephone,MessageOut.messagecontent,MessageOut.messagetime,";
			sql += " DeliverStation.reportState from RespStation ";
			sql += " left join MessageOut on MessageOut.messagesend = RespStation.sendId ";
			sql += " left join DeliverStation on DeliverStation.msgContent = RespStation.submitReserve group by MessageOut.id";
			
			Query query = s.createSQLQuery(sql);
			query.setFirstResult(startRow1);
			query.setMaxResults(pageSize1);
			return query.list();
		}
	});

}

}